
// 天选模式


let { findById } = require('../utils/array')



module.exports = function(io, setTheCup){

	setInterval(() => {
		setTheCup.user.sort(() => 0.5 - Math.random())
	}, 60000)

	let xSum = 60// x轴40个格子
    let ySum = 60// y轴40个格子

	let sum = ySum * xSum

	let maxFood = sum * 0.3

	let setTheCupSocket = io.of(setTheCup.Path)

	move()

	let setInta = null

	// 行动
	function move(){
		let ss = 0
		setInterval(() => {

			ss++

			if (ss >= 20 / setTheCup.user.length) {
				ss = 0
				生成食物(Math.floor(Math.random() * sum))
			}

			setTheCup.user.forEach(item => {

				item.seep++

				if (item.seep % 2 == 0) {
					if (item.accelerate) {
						if (item.seep % 10 != 0) {

						}else if (item.body.length > 2) {
							item.body.splice(0, 1)
							生成食物(Math.floor(Math.random() * sum))
						}else{
							item.accelerate = false
							return
						}
					}else{
						return
					}
				}

				

				for(let i = 0;i < item.body.length - 1;i++){
					item.body[i] = item.body[i + 1]
				}
				
				let so = item.body[item.body.length - 1] + item.course

				if (so >= sum || so < 0 || 检查是否撞到其他蛇的身体(so, item.id)) {
					let i = 生成出生位置()

					item.body.forEach(n => {
						生成食物(n)
					})

					item.body = [i, i + 1]
				}else{
					item.body[item.body.length - 1] = so
				}

				let i = setTheCup.food.indexOf(item.body[item.body.length - 1])
	
				// 吃到食物
				if(i != -1){
					let f = setTheCup.food.splice(i, 1)
					item.body.unshift(f[0])
				}
			})

			setTheCupSocket.emit('draw', setTheCup)
		}, 80)
	}



	setTheCupSocket.on('connection', function (socket) {

		socket.emit('setId', socket.id)

		let i = 生成出生位置()

		加入蛇({
			id : socket.id,
			body : [i, i + 1],
			seep : 0,
			course : 1
		})

		socket.on('course', (course) => {
			findById(setTheCup.user, socket.id).course = course
		})

		socket.on('setName', (userName) => {
			findById(setTheCup.user, socket.id).userName = userName
		})

		socket.on('accelerate', () => {
			let e = findById(setTheCup.user, socket.id)
			e.accelerate = !e.accelerate
		})

		// 断开链接
		socket.on('disconnect', ()=>{
			for(let i = 0;i < setTheCup.user.length;i++){
				if(setTheCup.user[i] && setTheCup.user[i].id == socket.id){
					if (setTheCup.user[i].body) {
						setTheCup.user[i].body.forEach(n => {
							生成食物(n)
						})
					}
					setTheCup.user.splice(i, 1)
				}
			}
		});
	});

	function 生成出生位置(){
		return Math.floor(Math.random() * (sum - xSum / 2)) + xSum / 4
	}

	function 加入蛇(data){
		if(!findById(setTheCup.user, data.id)){
			setTheCup.user.push(data)
		}
	}

	function 生成食物(food){

		if (setTheCup.food.length > maxFood) {
			return
		}

		if (!food && food != 0) {
			food = Math.floor(Math.random() * sum)
		}

		if (setTheCup.food.indexOf(food) == -1) {
			setTheCup.food.push(food)
		}
	}

	function 检查是否撞到其他蛇的身体(head, id){
		for (let index = 0; index < setTheCup.user.length; index++) {
			const element = setTheCup.user[index];
			if (element.id == id) {
				break
			}
			for (let index = 0; index < element.body.length; index++) {
				const e = element.body[index];
				if (e == head) {
					return true
				}
			}
		}
	}

}